package cn.restcloud.springboot.init;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import cn.restcloud.framework.core.util.ConfigUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.dispatcher.ActionDispatcher;
import cn.restcloud.springboot.config.SpringbootWebMvcConfig;

/**
 * 主要处理/restcloud的api请求
 * @author Thinkpad
 *
 */

public class RestCloudServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private String prefixPath=SpringbootWebMvcConfig.getRootPath(); //要去掉路径前缀再匹配服务地址
	public static String asyncsupported=ConfigUtil.getLocalConfig("async.supported","false"); //异步支持设为true时表示支持异步
	public static String asyncThreadNum=ConfigUtil.getLocalConfig("async.thread.maxnum","100"); //默认开启100个线程
	public static String asyncTimeOut=ConfigUtil.getLocalConfig("async.thread.timeout","300000"); //默认超时设置
	private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor=new ScheduledThreadPoolExecutor(Integer.parseInt(asyncThreadNum));
	private static String webappsProjectName=ConfigUtil.getLocalConfig("webapps.project.name",""); //默认部署在root目录下，如果部署在其他目录下则需要指明目录名称
	
	/**
	 * 获取url调度器
	 * @return
	 */
	private ActionDispatcher getActionDispatcher() {
		return new ActionDispatcher();
	}
	
	/**
	 * 获取线程池
	 * @return
	 */
	public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() {
		//System.out.println("异步支持:"+asyncsupported+" 异步处理线程数:"+asyncThreadNum+" 异步超时设置为:"+asyncTimeOut);
		//return new ScheduledThreadPoolExecutor(Integer.parseInt(asyncThreadNum)); //线程池
		return scheduledThreadPoolExecutor;
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestUrl=getRequestUrl(request);
		if(asyncsupported.equals("true")){
			//异步请求支持
			AsyncContext asyncContext = request.startAsync(request, response);
			asyncContext.setTimeout(Integer.parseInt(asyncTimeOut)); //默认30秒超时
			getScheduledThreadPoolExecutor().execute(new ActionDispatcher(asyncContext,requestUrl));
		}else{
			//同步处理模式
			getActionDispatcher().execute(request, response,false,requestUrl,"",null);
		}
		//System.out.println("核心线程数" + handlerExecutor.getCorePoolSize());
		//System.out.println("线程池数" + handlerExecutor.getPoolSize());
		//System.out.println("队列任务数" + handlerExecutor.getQueue().size());
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  doGet(request,response);
	}
  
	@Override
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  doGet(request,response);
	}
	
	@Override
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  doGet(request,response);
	}
	
	/**
	 * 获取请求的服务地址,去掉rootpath前面一段的服务地址
	 * @param request
	 * @return
	 */
	public String getRequestUrl(HttpServletRequest request){
		String url =request.getRequestURI().replace("//","/");
		if(StringUtils.isBlank(webappsProjectName)) {
			url=url.substring(prefixPath.length(),url.length());//访问地址:http://localhost/restcloud/rest/appid/test.do?id=1得到/rest/appid/test.do
		}else {
			url=url.substring(webappsProjectName.length(),url.length());//访问地址:http://localhost/restcloud/rest/appid/test.do?id=1得到/rest/appid/test.do
		}
		return url;
	}

	/**
	 * 获取项目部署的路径名称，如果是在root目录下则应该为空值
	 * @return
	 */
	protected static String getWebappsProjectName() {
		return webappsProjectName;
	}

	
}